<template>
  <div>
    <workflow-editor 
        :data.sync="data"
        @ok="ok"/>
  </div>
</template>

<script>
import {getAppWorkflowNodes, updateWorkflow} from "@/api/appWorkflow"
import { treeToLink } from './components/node/process'
import WorkflowEditor from './components/node/Main.vue'
export default {
  components: {
    WorkflowEditor
  },
  methods: {
    async ok (data, published) {
      if(published){
        await updateWorkflow({
          id: this.id,
          published
        })
        this.$router.back()
        return
      }
      let definition = treeToLink(data.node)
      await updateWorkflow({
        id: this.id,
        definitionTree: data,
        definition,
      })
    }
  },
  data(){
      return {
          data: undefined
      }
  },
  computed:{
      id(){
          return this.$route.params.id
      }
  },
  async created(){
    let data = await getAppWorkflowNodes()
    this.$store.commit('updateNodeDefinitions', data)
  }
}
</script>